#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask_restful import Resource, reqparse
from flask import jsonify,g ,make_response

from service.planService import PlanService
from controller.planController import PlanController
from utils import commons
from utils.response_code import RET
from utils.response_code import RET
from utils.verify_operate import verify_user_role

class PlanOtherResource(Resource):

    @classmethod
    def sensitive_update(cls, plan_id):
        if not plan_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('is_delete', location='form', required=False, help='is_delete参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['plan_id'] = plan_id

        res = PlanController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])


    @classmethod
    def save_plan(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('plan_quantity', type=int, location='form', help='plan_quantity 参数类型不正确')
        parser.add_argument('plan_time', type=int, location='form', help='plan_time 参数类型不正确')
        parser.add_argument('dictionary_id', type=int, required=True, location='form',
                            help='dictionary_id 参数类型不正确或缺失')
        parser.add_argument('plan_type', type=int, required=True, location='form',
                            help='plan_type 参数类型不正确或缺失')
        parser.add_argument('user_id', type=int, required=True, location='form', help='user_id 参数类型不正确或缺失')

        kwargs = parser.parse_args()
        # 去除空值
        kwargs = commons.put_remove_none(**kwargs)
        result = PlanService.save_plan(**kwargs)

        return jsonify(code=result['code'], message=result['message'], data=result['data'])

    @classmethod
    def delete_plan(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('user_id', type=int, required=True, location='form',
                            help='user_id 参数类型不正确或缺失')
        parser.add_argument('dictionary_id', type=int, required=True, location='form',
                            help='dictionary_id 参数类型不正确或缺失')

        kwargs = parser.parse_args()
        # 去除空值
        kwargs = commons.put_remove_none(**kwargs)

        result = PlanService.delete_plan(kwargs)

        return jsonify(code=result['code'], message=result['message'], data=result['data'])
